package org.pb;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.springframework.web.client.RestTemplate;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
 
public class MyArchivalOrders extends ListActivity implements OnItemClickListener{
	 
		private Map<String, Order> oldOrders;
		private Button archivalOrders;
		
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        try{
	        this.manageOrders(this.getOrders());
	        
	        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.prepareListToShow()));


	        setContentView(R.layout.myarchivalorders);
	       
	        ListView lv = getListView();
	        lv.setTextFilterEnabled(true);

	        lv.setOnItemClickListener(this);}
	        catch(Exception e){}
	    } 
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.TestButton:
				Intent i= new Intent(getApplicationContext(), MyArchivalOrders.class);
	this.startActivity(i);
				break;}
	}

		private Order[] getOrders() {
			Order[] orders;

			RestTemplate restTemplate = TaxiApplication.getRestTemplate();
			try {
				  SharedPreferences settings = getSharedPreferences(TaxiApplication.PREFS_NAME, 0);

	//
			/*	  
			orders = new Order[100];	  
			for (int i=0;i<=99;i++){	  
				  Order o = new Order();
				  o.setComment("Brak komentarza");
				  o.setIdPerson(12);
				  o.setOrderStatus(5);
				  o.setRegTimestamp(new Date());
				  o.setLatitude(52.22);
				  o.setLongitude(21.01);
				  o.setExecutionTimestamp(new Date());
			orders[i]=o;
			}	  
				*/  
				  //

					String BASE_URL=settings.getString("BASEURL",null);	
		orders = restTemplate.getForObject(BASE_URL+TaxiApplication.GETALLORDERS_URL+settings.getString("idPerson", null) + ".json", Order[].class);
		//		orders = restTemplate.getForObject(TaxiApplication.GETALLORDERS_URL+14 + ".json", Order[].class);

//		  orders = restTemplate.getForObject("http://dl.dropbox.com/u/151859/aaa.json", Order[].class);

			} catch (Exception e) {
				orders = null;

			}
			return orders;
		}
		private void  manageOrders(Order[] orders){
			oldOrders=new HashMap<String, Order>();
int i=1;
			for (Order o:orders){
				if(o.getOrderStatus()!=0){
					oldOrders.put(i+"."+o.getRegTimestamp().toGMTString(), o);
				}
i++;
			}
			
		}
		private String[] prepareListToShow(){
			String[] list={};

			if(oldOrders.size()!=0){
				list = new String[oldOrders.size()];
				Set keySet=oldOrders.keySet();
				Iterator it = keySet.iterator();
				int i=0;
				while(it.hasNext()){
					list[i]=(((String)it.next()));
							i++;
				}
			}

			return list;
			
		}



		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			
			
		
			String date = (String)this.getListView().getItemAtPosition(arg2);  
	//		  StringTokenizer st = new StringTokenizer(date,".");
	//st.nextToken();
	//date=st.nextToken();
	//date=date.substring(1);
		//	Toast.makeText(this, date,
		//			Toast.LENGTH_SHORT).show();
		//	SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
		//	Date date=null;
//			try {  
		//	   date = format.parse(dtStart);  
		//	    System.out.println(date);  
//			} catch (ParseException e) {  
			    // TODO Auto-generated catch block  
		//	    e.printStackTrace();  
		//	}
			
		//	Toast.makeText(this, date.toGMTString(),
		//			Toast.LENGTH_SHORT).show();
			
			Order o = oldOrders.get(date);
		//		Toast.makeText(this, o.getComment(),
		//				Toast.LENGTH_SHORT).show();
			
			
			
			Intent i= new Intent(getApplicationContext(), ShowArchivalOrder.class);
			i.putExtra("regTimestamp",o.getRegTimestamp().toGMTString());
		//	i.putExtra("regTimestamp","7 Jun 2012 15:30:01 GMT");

			i.putExtra("startLongitude",o.getLongitude().toString());
			i.putExtra("startLatitude",o.getLatitude().toString());
	
			//	i.putExtra("stopLatitude",o.getStopLatitude());
		//	i.putExtra("stopLongitude",o.getStopLongitude);
			i.putExtra("stopLatitude","52.24308");
			i.putExtra("stopLongitude","21.01345");
		
			if(!o.getOrderStatus().toString().equals("0")){
				i.putExtra("orderStatus", "Zlecenie zrealizowane");
			}
		//	i.putExtra("finishTimestamp",o.getFinishTimestamp().toGMTString());
			i.putExtra("finishTimestamp","7 Jun 2012 15:47:34 GMT");
		
			i.putExtra("comment",o.getComment());
			i.putExtra("executionTimestamp",o.getExecutionTimestamp().toGMTString());
		//	i.putExtra("executionTimestamp","7 Jun 2012 15:30:15 GMT");

			startActivity(i);
			
			
		}
	    
	}



